﻿using GalaSoft.MvvmLight.Messaging;
using Reactive.Bindings;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using WPF_ReactiveProperty_PathSearchUtil.ViewModels.common;

namespace WPF_ReactiveProperty_PathSearchUtil.ViewModels
{
    public class FindFormViewModel
    {
        public ReactivePropertySlim<string> FindWord { get; }
        public Window CurrentWindow { get; set; }
        public ReactiveCommand ConfirmButton { get; set; }

        public ReactiveCommand CancelButton { get; set; }
        public ReactiveCommand<KeyEventArgs> KeyDownEvent { get; set; }

        public FindFormViewModel(Window window)
        {
            CurrentWindow = window;
            FindWord = new ReactivePropertySlim<string>();
            ConfirmButton = new ReactiveCommand();
            CancelButton = new ReactiveCommand();
            KeyDownEvent = new ReactiveCommand<KeyEventArgs>();
            ConfirmButton.Subscribe(x => { Confirm(); });
            CancelButton.Subscribe(x => { Cancel(); });
            KeyDownEvent.Subscribe(KeyDown);
        }

        public FindFormViewModel()
        {
        }

        public void Confirm()
        {
            if (FindWord.Value == null)
            {
                MessageBox.Show("请输入要检索的文件名。", "提示");
                return;
            }
            // 创建自定义消息对象
            var message = new MyMessage
            {
                Messages = FindWord.Value
            };
            CurrentWindow.Close();
            // 发送消息给所有已注册的接收者
            Messenger.Default.Send(message);
        }

        public void Cancel()
        {
            CurrentWindow.Close();
        }

        /// <summary>
        /// keyDown
        /// </summary>
        /// <param name="e"></param>
        public void KeyDown(KeyEventArgs e)
        {
            if (e.Key == Key.Escape) Cancel();;
            if (e.Key == Key.F8) Confirm();
        }
    }
}
